// pages/product/curselist/curselist.js
let AJAX = require("../../../utils/util.js").AJAX
let productKind = require("../../../utils/util.js").productKind
let timeTranfer = require("../../../utils/util.js").timeTranfer
const App = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navH:"",
    typeList:[],
    activeNum: '',
    productList: [],
    page: 1,
    total: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      navH: App.globalData.navHeight,
      activeNum: options.id || ''
    })
    this.requestType()
    this.requestList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  navBack(){
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  requestType() {
    let vm = this
    AJAX("product/category", "GET", {
      product_type: 1
    }).then(res => {

      let _data = [{ category_id: "", category_name: "全部" }]
      if (res.data) {
        for (let i in res.data) {
          _data.push(res.data[i])
        }
      }
      vm.setData({
        typeList: _data
      })
    })
  },
  tabActive(e) {
    let _id = e.currentTarget.dataset.type
    this.setData({
      activeNum: _id,
      page: 1
    })
    this.requestList()
  },
  requestList(str) {
    let vm = this
    AJAX("product/list", "GET", {
      category_id: vm.data.activeNum || '',
      page: vm.data.page,
      product_type:1
    }).then(res => {
      let _data = []
      if (str) {
        _data = vm.data.productList
      }
      res.data.forEach(function (item) {
        item.product_kind = productKind[item.product_kind || 1]
        item.theme_name = item.theme_name

        if (item.theme_name) {
          item.theme_name = item.theme_name.split('|')
        }
        item.begin_time = timeTranfer(item.begin_time, 1)
        item.end_time = timeTranfer(item.end_time, 1)
        _data.push(item)
      })
      vm.setData({
        productList: _data,
        total: res.header["X-Pagination-Total-Count"]
      })
    })
  },
  toInfo(e){
    let _id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/product/curseinfo/curseinfo?id='+_id,
    })
  }
})